﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Addressbook
{
    public partial class Form1 : Form
    {
        List<Person> myAddressbook;
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillWithSampleData();
            FillList();
        }

        /// <summary>
        /// Fill Samplelist
        /// </summary>
        private void FillWithSampleData()
        {
            try
            {
                using (var db = new Addressbook())
                {
                    myAddressbook = new List<Person>{
                        new Person (
                            1,
                            "Marge",
                            "Simpson", 
                            new List<Address> {
                                new Address("Street1","IMX YYY"),
                                new Address("Street2","IMX ZZZ")
                            }
                        ),
                        new Person (
                            id: 2,
                            firstname: "Homer",
                            lastname: "Simpson", 
                            addresslist: new List<Address> {
                                new Address("StreetDemo","IMX YYY")
                            }
                        )
                    };

                    //add another address
                    Address myfirstaddress = new Address("samplestreet", "samplepostcode");
                    List<Address> mylistofAddresses = new List<Address> { myfirstaddress };
                    Person myperson = new Person(1, "Andrea", "Lener", mylistofAddresses);
                    myperson.Age = 200;
                    myAddressbook.Add(myperson);

                    //save in entity object
                    foreach (Person person in myAddressbook)
                    {
                        db.Persons.Add(person);
                    }
                    //save in database.
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void InitListbox()
        {
            this.grdAddressList.Columns.Clear();
            this.grdAddressList.Columns.Add("Id", 150, HorizontalAlignment.Left);
            this.grdAddressList.Columns.Add("FirstName", 150, HorizontalAlignment.Left);

            this.grdAddressList.Items.Clear();
        }

        private void AddPersonToGrid(Person person)
        {
            ListViewItem parent;
            parent = this.grdAddressList.Items.Add(person.Id.ToString());
            parent.SubItems.Add(person.Firstname);
        }

        private void FillList()
        {
            InitListbox();
            foreach (Person person in myAddressbook)
            {
                AddPersonToGrid(person);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //using (var db = new Addressbook())
            //{
            //    var address = new Address { Name = name };
            //    db.Blogs.Add(blog);
            //    db.SaveChanges();

            //    // Display all Blogs from the database 
            //    var query = from b in db.Blogs
            //                orderby b.Name
            //                select b;

            //    Console.WriteLine("All blogs in the database:");
            //    foreach (var item in query)
            //    {
            //        Console.WriteLine(item.Name);
            //    }

            //    Console.WriteLine("Press any key to exit...");
            //    Console.ReadKey();
            //} 
        }

    }
}
